using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class SortEventPage : System.Web.UI.Page
{
    SqlDataSource sqlDataSource;    
    protected void Page_Load(object sender, EventArgs e)
    {
         
        if (!IsPostBack)
        {
            FillRecruiterAccount(GridView1, "Email", true);
            ViewState["SortAscending"]=true;    
        }
    }

    void FillRecruiterAccount(GridView gridView, string expressionName, bool sortAscending)
    {
        sqlDataSource = new SqlDataSource();
        sqlDataSource.ConnectionString =
        WebConfigurationManager.ConnectionStrings[
            "RecruitVietnamDbConnectionString"
            ].ConnectionString;
        sqlDataSource.SelectCommandType =
        SqlDataSourceCommandType.Text;
        sqlDataSource.SelectCommand = "select " +
        "Email,CompanyName, "
        + "ContactName,Address,Activate"+
        " from tblRCAccounts  order by ["
         + expressionName + "]  " + (sortAscending?" ASC":"DESC");
        gridView.DataSource = sqlDataSource;
        gridView.DataBind();
        

    }
     
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
         
        bool sortAscending =  (ViewState["SortAscending"]==null)? 
        true:(bool)ViewState["SortAscending"];
        if (sortAscending)
            {
            e.SortDirection = SortDirection.Ascending;
            sortAscending = false;
            }
        else
            {
                e.SortDirection = SortDirection.Descending;
                sortAscending = true;
            }

        FillRecruiterAccount(GridView1, e.SortExpression, sortAscending);        
        ViewState["SortAscending"]= sortAscending;
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        bool sortAscending = (ViewState["SortAscending"] == null) ?
               true : (bool)ViewState["SortAscending"];         
        GridView1.PageIndex = e.NewPageIndex;
        FillRecruiterAccount(GridView1, "Email", sortAscending);
    }
}
